home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_craters.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  3.7 KB  |  117 lines

  1. /*******************************************************************************
  2.  * JBUMcraters.sl -- Lunar-like surface - Jim Bumgardner jbum@jbum.com
  3.  *
  4.  * Voronoi/Worley noise is combined using 1/f scaling to put craters on a surface.
  5.  * Similar tricks can be used to make cork, swiss-cheese, bread, etc.
  6.  ******************************************************************************/
  7.  
  8.  
  9. #include "k3d_material.h"
  10. #include "k3d_noises.h"
  11. #include "k3d_patterns.h"
  12.  
  13.  
  14. surface
  15. k3d_craters ( float Ka = 1, Kd = 0.7, roughness = 0.1;
  16.    float Km = 0.03;    /* Depth of crater displacement */
  17.    float swidth=.6;      /* Size of largest crater relative to s/t mapping */
  18.    float pitfactor = .7; /* chance of crater occuring in a cell */
  19.    float octaves=9;      /* number of scales used - use lower numbers for cartoony effects */
  20.    float lac=1.91341;    /* diameter spacing between successive octaves */
  21.    float amp=1.91341;    /* amplitude reduction between successive octaves */
  22.    float jitter=1.0;     /* irregularity of crater spacing */
  23.   
  24.    /* you'll get artifacts if you go too much higher than 1 */
  25.    /* but this can be avoided by looping i,j from -2 to 2 instead of -1 to 1 */
  26.    float distortamp = 0.0005; /* Crater distortion.  You'll get artifacts if it goes too high. */
  27. )
  28. {
  29.     normal Nf;
  30.     color        Ct;
  31.  
  32.     float ss, tt, angle, r, rim, uu, a, i, j, sc, asc, lev;
  33.     float sctr, tctr, scell, tcell,scellctr,tcellctr;
  34.     float pert=0,rad,pert2;
  35.     float rmax = swidth/2; /* was /2 */
  36.     float    rmax1=1000000,rmax2=1000000,rrad1=0,rrad2=0,cellsizescale,ilac;
  37.  
  38.     ilac = 1/lac;
  39.  
  40.     /* Add craters */
  41.  
  42.     for (lev = 0; lev < octaves; lev += 1)
  43.       {
  44.     sc = pow(lac, lev);
  45.     asc = 1 / pow(amp, lev);
  46.     scellctr = floor(s*sc/swidth);
  47.     tcellctr = floor(t*sc/swidth);
  48.     cellsizescale = swidth/sc;
  49.     rrad1 = 0;
  50.     rrad2 = 0;
  51.     for (i = -1; i <= 1; i += 1) 
  52.       {
  53.         scell = scellctr + i;
  54.         for (j = -1; j <= 1; j += 1) 
  55.           {
  56.         tcell = tcellctr + j;
  57.         if ( cellnoise(7*scell-9.5, 7*tcell+7.5) < pitfactor)
  58.           /* xp = (sc*7*scell-9.5,sc*7*tcell+7.5,.5); */
  59.           /* if ( fBm_default(xp) < pitfactor)  *//* I like this distribution better - pitf = -1 -> 1 */
  60.           {
  61.             sctr = cellsizescale * (scell + 0.5 + jitter * cellnoise(scell+0.5, tcell+0.5));
  62.             tctr = cellsizescale * (tcell + 0.5 + jitter * cellnoise(scell+3.5, tcell+8.5));
  63.             ss = s - sctr;
  64.             tt = t - tctr;
  65.             r = ss*ss + tt*tt; /* r is distance from center squared */
  66.             if (r < rmax1) {
  67.               /* rad is size of crater squared */
  68.               rad = rmax*ilac;
  69.               rad /= sc;
  70.               rad *= rad;
  71.               rmax2 = rmax1;
  72.               rrad2 = rrad1;
  73.               rmax1 = r;
  74.               rrad1 = rad;
  75.             }
  76.             else if (r < rmax2) {
  77.               /* rad is size of crater squared */
  78.               rad = rmax*ilac;
  79.               rad /= sc;
  80.               rad *= rad;
  81.               rmax2 = r;
  82.               rrad2 = rad;
  83.             }
  84.           }
  85.           }
  86.       }
  87.     /* Optional Crater Distortion... */
  88.     if (distortamp != 0)
  89.       {
  90.         rrad1 += fBm_default(P*sc)*asc*distortamp; /* add crater roughness */
  91.         rrad2 += fBm_default(P*sc)*asc*distortamp; 
  92.       }
  93.     
  94.     /* Seem crater perturbations for each scale */
  95.     if (rrad1 > 0 && rmax1 > 0) 
  96.       {
  97.         r = smoothstep(0,rrad1,rmax1);
  98.         r = r*(2*r-pow(r,3))+(1-r)*pow(r,3);
  99.         pert += sin(r*PI/2)*asc-asc;
  100.       }
  101.     if (rrad2 > 0 && rmax2 > 0) 
  102.       {
  103.         r = smoothstep(0,rrad2,rmax2);
  104.         r = r*(2*r-pow(r,3))+(1-r)*pow(r,3);            
  105.         pert += sin(r*PI/2)*asc-asc;
  106.       }
  107.       }
  108.     
  109.     P += Km*pert*normalize(N);
  110.     N = calculatenormal(P);    
  111.     Nf = faceforward (normalize(N),I);
  112.     
  113.     Ct = Cs;
  114.     Ci = MaterialClay (Nf, Ct, Ka, Kd, roughness);
  115.     Oi = Os;  Ci *= Oi;
  116. }
  117.